#include <stdio.h>
#include <malloc.h>
#include <bfd.h>
#include "load.h"


int load_exec(addr_type_t type, char *file){
	
	int ret = -1;
	bfd *bfd_info = NULL;
	asection *s = NULL;
	unsigned char* sec_data = NULL;


	bfd_info = bfd_openr(file,NULL);

	if(bfd_info == NULL){
		fprintf(stderr,"open %s error : %s\n",file,bfd_errmsg(bfd_get_error()));
		return ret;
	}

	if(bfd_check_format(bfd_info,bfd_object) != TRUE){
		fprintf(stderr,"format error %s : %s\n",file, bfd_errmsg(bfd_get_error()));
		return ret;
	}


	for(s = bfd_info->sections; s; s++){

		if(bfd_get_section_flags(bfd_info,s) & (SEC_LOAD)){
			if(bfd_section_size(bfd_info,s) > 0){
				sec_data = malloc(bfd_section_size(bfd_info,s));

			if(sec_data == NULL){
				fprintf(stderr,"section %s's memory allocation error\n",bfd_section_name(bfd_info,s));
				return ret;
			}
		}
			else {
				fprintf(stderr,"section : %s's size is zero\n",bfd_section_name(bfd_info,s));
				return ret;
			}

		if(bfd_get_section_contents(bfd_info,s,sec_data,0,bfd_section_size(bfd_info,s)) == FALSE){
				fprintf(stderr,"get section %s content error %s\n",
					bfd_section_name(bfd_info,s),
					bfd_errmsg(bfd_get_error()));
				return ret;
			}

			if(type == physical_addr){


			}
			else if(type == virtual_addr){


			}
			else {


			}

			free(sec_data);
			sec_data = NULL;
		}
	}

}
